# -*- coding: utf-8 -*-
"""
author:码同学 极光
date:2024/3/24
desc: 
sample: 
"""
#token
import json

import jsonpath as jsonpath
import requests

#txt-》  html/xml 怎么处理 正则  #json
host = 'http://121.11.97.201:9088/'
def token():
    url =host+'/pinter/bank/api/login2'
    data={'userName':'admin','password':'1234'}
    res = requests.post(url=url,data=data)
    print(res.json().get('data'))
    #返回结果字典处理
    token = res.json().get('data')
    #返回结果jsonpath获取 复杂json
    token= jsonpath.jsonpath(res.json(), '$.data')[0]
    assert res.status_code == 200
    assert res.json()['code'] == '0'
    url=host+'/pinter/bank/api/query2?userName=admin'
    headers = {
        'testfan-token': token
    }
    res= requests.get(url,headers=headers)
    print(res.text)


if __name__ == '__main__':
    token()
    json_str='{"store":{"book":' \
         '[{"category":"reference","author":"Nigel Rees","title":"Sayings of the Century",' \
         '"price":8.95},{"category":"fiction",' \
             '"author":"J. R. R. Tolkien","title":"The Lord of the Rings","isbn":"0-395-19395-8","price":22.99}],' \
         '"bicycle":{"color":"red","price":19.95}}}'
    print(type(json_str))
    books=json.loads(json_str)
    print(type(books))
    checkurl = "$.store.bicycle.color" #绝对路径
    print(jsonpath.jsonpath(books, checkurl))
    checkurl = "$..color"  # 相关路径
    print(jsonpath.jsonpath(books, checkurl))

    checkurl = "$.store.book[*]"
    object_list = jsonpath.jsonpath(books, checkurl)
    print(object_list)

    checkurl = "$.store.book[0]"
    obj = jsonpath.jsonpath(books, checkurl)
    print(obj)

    checkurl = "$.store.book[*].title"
    titles = jsonpath.jsonpath(books, checkurl)
    print(titles)






